package com.wmh.baseservice.admin.controller;

import com.wmh.baseservice.admin.entity.AdInfo;
import com.wmh.baseservice.admin.pojo.vo.user.CreateAdminReqVO;
import com.wmh.baseservice.admin.pojo.vo.user.GiveRoleReqVO;
import com.wmh.baseservice.admin.pojo.vo.user.ResetPwdReqVO;
import com.wmh.baseservice.admin.pojo.vo.user.UpdateAdminReqVo;
import com.wmh.baseservice.admin.service.AdInfoService;
import com.wmh.baseservice.admin.service.AdUserRoleService;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.common.utils.web.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author mmx generator
 * @since 2021-02-03
 */
@Api(tags = "管理员-账号信息相关接口")
@Validated
@RestController
@RequestMapping("superAdmin/adInfo")
public class AdInfoController {
    @Resource
    private AdInfoService adInfoService;

    @Resource
    private AdUserRoleService adUserRoleService;

    @Resource
    private UserUtil userUtil;


    @ApiOperation("创建管理员账号")
    @PostMapping("create")
    @RequiresPermissions("admin.create")
    public String create(@Valid @RequestBody CreateAdminReqVO reqVO) {
        adInfoService.create(reqVO);
        return ReturnBody.success();
    }

    @ApiOperation("删除账号")
    @PostMapping("delete")
    @RequiresPermissions("admin.delete")
    public String delete(Long adId) {
        adInfoService.delete(adId);
        return ReturnBody.success();
    }

    @ApiOperation("修改管理员账号")
    @PostMapping("update")
    @RequiresPermissions("admin.update")
    public String update(@Valid @RequestBody UpdateAdminReqVo reqVo) {
        adInfoService.update(reqVo);
        return ReturnBody.success();
    }

    @ApiOperation(value = "重置管理员密码密码")
    @PostMapping("resetPassword")
    @RequiresRoles("超级管理员")
    public String resetPassword(@Valid @RequestBody ResetPwdReqVO reqVO) {
        adInfoService.resetPassword(reqVO);
        return ReturnBody.success();
    }

    @ApiOperation("修改账号状态")
    @PostMapping("updateStatus")
    @RequiresPermissions("admin.update")
    public String updateStatus(@NotNull(message = "未提交主键")Long adId,@NotNull(message = "未提交状态") Integer status){
        adInfoService.updateStatus(adId,status);
        return ReturnBody.success();
    }

    @ApiOperation("对管理员给予角色")
    @PostMapping("giveRole")
    @RequiresPermissions("admin.giveRole")
    public String giveRole(@Valid @RequestBody GiveRoleReqVO reqVO) {
        adInfoService.giveRole(reqVO);
        return ReturnBody.success();
    }

    @ApiOperation("账号列表")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "account", value = "账号", required = false),})
    @GetMapping("list")
    @RequiresPermissions("admin.query")
    public String list(@Valid PageInfo pageInfo, String account, String name, String remark) {
        Long id = userUtil.getId();
        //执行分页辅助工具
        PageResult<AdInfo> pageResult = new PageUtil<AdInfo>(pageInfo).startPage((page, queryWrapper) -> {
            if (CommUtil.checkNull(account)) {
                queryWrapper.eq(AdInfo.ACCOUNT, account);
            }
            if (!StringUtils.isBlank(name)) {
                queryWrapper.like(AdInfo.NAME, name);
            }
            if (!StringUtils.isBlank(remark)) {
                queryWrapper.like(AdInfo.REMARK, remark);
            }
            queryWrapper.ne(AdInfo.ID, id);
            queryWrapper.select(AdInfo.getFields(AdInfo.SALT, AdInfo.PASSWORD));
            adInfoService.page(page, queryWrapper);
        });
        return ReturnBody.success(pageResult);
    }

    @ApiOperation("获取管理员所有用的角色编号")
    @GetMapping("getRoleIds")
    public String getRoleIds(Long adId) {
        return ReturnBody.success(adUserRoleService.getRoleIdsByCache(adId));
    }

    @ApiOperation("获取管理员的基础信息")
    @GetMapping("adminSimpleInfo")
    public String getAdminSimpleInfo() {
        return ReturnBody.success(adInfoService.getSimpleInfo(userUtil.getId()));
    }
}
